﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShopControl : MonoBehaviour
{
	public Sprite sprButtonHp_CanBuy, sprButtonHp_CanNotBuy, sprButtonAtk_CanBuy, sprButtonAtk_CanNotBuy;
	public Text tAtkPrice, tHpPrice;
	public Image bHpImage, bAtkImage;
	int _atkPrice;
	// gia tien upgrade atk
	public int atkPrice {
		get { return _atkPrice; }
		set { 
			_atkPrice = value;
			tAtkPrice.text = "$ " + value;
			// set sprite cho nut mua theo so tien dang co
			if (GameManager.dataSave.coin < value) {
				bAtkImage.sprite = sprButtonAtk_CanNotBuy;
			} else {
				bAtkImage.sprite = sprButtonAtk_CanBuy;
			}
		}
	}

	int _hpPrice;
	// gia tien upgrade hp
	public int hpPrice {
		get { return _hpPrice; }
		set { 
			_hpPrice = value;
			tHpPrice.text = "$ " + value;
			// set sprite cho nut mua theo so tien dang co
			if (GameManager.dataSave.coin < value) {
				bHpImage.sprite = sprButtonHp_CanNotBuy;
			} else {
				bHpImage.sprite = sprButtonHp_CanBuy;
			}
		}
	}

	void Awake ()
	{
		InitPrice ();
	}

	void OnEnable ()
	{
		GameManager.gameState = GameState.SHOPPING;
	}

	void InitPrice ()
	{
		// tinh toan gia tien cac item
		hpPrice = (GameManager.dataSave.hpAdd / 5 + 1) * 3;
		atkPrice = (GameManager.dataSave.atkAdd / 3 + 1);
	}

	public void UpgradeHp ()
	{
		if (GameManager.dataSave.coin >= hpPrice) {
			AudioManager.instance.PlaySound (AudioClipType.shop);
			GameManager.dataSave.coin -= hpPrice;
			GameManager.dataSave.hpAdd += 5;
			InitPrice ();
			SelectLevelControl.instance.UpdateCoinText ();
			GameManager.SaveData ();
		}
	}

	public void UpgradeAtk ()
	{
		if (GameManager.dataSave.coin >= atkPrice) {
			AudioManager.instance.PlaySound (AudioClipType.shop);
			GameManager.dataSave.coin -= atkPrice;
			GameManager.dataSave.atkAdd += 3;
			InitPrice ();
			SelectLevelControl.instance.UpdateCoinText ();
			GameManager.SaveData ();
		}
	}

	public void Close ()
	{
		gameObject.SetActive (false);
		GameManager.gameState = GameState.SELECT_LEVEL;
	}
}
